var vm = new Vue({
    el: '#app',
    data: {
        list: '',
        id: '',
        userList: ''
    },
    methods:{
        getRole(){
            $.get('role/list', function (res) {
                vm.list = res
            });
        },
        getTreeData(){
            $("#addRoleTree").tree({
                url:'role/tree',
                method:'get',
                checkbox: true,
                state: 'closed',
                lines: true,
                animate:true,
                formatter: function (data) {
                    return data.status == 0 ? data.name + "(冻结)" : data.name;
                },
            });
        },
        permission(index){

            this.id = this.list[index].id;

            var nodes = $("#addRoleTree").tree('getChecked');

            if (nodes.length != 0){

                for (var i=0; i<nodes.length; i++){

                    $("#addRoleTree").tree('uncheck', nodes[i].target);
                }
            }

            $.ajax({
                url: 'role/getAcl',
                data: {
                    role_id: this.id
                },
                type: 'post',
                success: function (res) {
                    if (res != undefined){
                        for (var i=0; i<res.length; i++){
                            var node = $("#addRoleTree").tree('find', res[i].id);
                            if (node != null && node.children.length == 0 ){
                                $("#addRoleTree").tree('check', node.target);
                            }
                        }
                    }
                }
            });

        },
        addRoleAcl(){

            var acl_id = new Array();

            var nodes1 = $("#addRoleTree").tree('getChecked');

            for (var i = 0; i < nodes1.length; i++) {
                acl_id.push(nodes1[i].id);
            }

            var nodes2 = $('#addRoleTree').tree('getChecked', 'indeterminate');

            for (var i = 0; i < nodes2.length; i++) {
                acl_id.push(nodes2[i].id);
            }

            $.ajax({
                url: 'role/addRoleAcl',
                data: {
                    ids: acl_id,
                    role_id: this.id
                },
                traditional:true,
                type: 'post',
                success: function (res) {

                }
            });
        },
        user(index){
            this.id = this.list[index].id;

            $.ajax({
                url: 'role/getUser',
                data: {
                    role_id: this.id
                },
                type: 'post',
                success: function (res) {
                    vm.userList = res
                }
            });
        },
        getUserTree(){
            $("#addUserTree").tree({
                url:'role/userTree',
                method:'get',
                checkbox: true,
                lines: true,
                animate:true,
                formatter: function (data) {
                    return data.username
                },
            });
        },
        addRoleUser(){

            var user_id = new Array();

            var nodes = $("#addUserTree").tree('getChecked');

            for (var i = 0; i < nodes.length; i++) {
                user_id.push(nodes[i].id);
            }

            $.ajax({
                url: 'role/addRoleUser',
                data: {
                    ids: user_id,
                    role_id: this.id
                },
                traditional:true,
                type: 'post',
                success: function (res) {

                }
            });
        }
    },
    filters:{
        typeFormat: function (str) {
            if (str == 1){
                return "管理员角色";
            } else {
                return "其他";
            }
        },
        statusFormat: function (str) {
            if (str == 1){
                return "有效";
            } else {
                return "冻结";
            }
        },
        stateFormat: function (str) {
            if (str == 1){
                return "有效";
            } else if(str == 0){
                return "冻结";
            }else {
                return "删除";
            }
        }
    },
    created(){
        this.getRole();
    },
    mounted(){
        this.getTreeData();
        this.getUserTree();
    },
});
